package com.geek.javase.ch06.test1;

import java.util.Arrays;
import java.util.Objects;

/*
 *@author wangpeng
 *2025/10/14 10:16
 *@description TODO
 */
public class Student {
    private String name;
    private String id;

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        /*return id == student.id && Objects.equals(name, student.name);*/
        return Objects.equals(id,student.id) && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id);
    }

    public Student(){
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }

    /**
     *  学生系统中，添加学生信息，并要求同名且同学号的则为同一人，不允许添加至系统中。
     *  定义一个添加方法，可以将学生添加至容器中。
     */
    public static class StudentTest {
        int index = 0;
        private Student[] students = new Student[10];
        //添加方法
        public boolean addStudent(Student student) {
            if(index >= students.length){
                students = Arrays.copyOf(students, students.length*3/2+1);
            }
            //遍历
            for (int i = 0; i < index; i++) {
                Student tempStudent = students[i];
                if(Objects.equals(student,tempStudent)){
                    return false;
                }
            }
            students[index++] = student;
            return true;
        }

        @Override
        public String toString() {
            return "StudentTest{" +
                    "index=" + index +
                    ", students=" + Arrays.toString(students) +
                    '}';
        }

        public static void main(String[] args) {
            Student s1 = new Student("13","张三");
            Student s2 = new Student("13","李四");
            Student s3 = new Student("16","张三");
            StudentTest st = new StudentTest();
            System.out.println(st.addStudent(s1));
            System.out.println(st.addStudent(s2));
            System.out.println(st.addStudent(s3));

            //System.out.println(st.toString());
        }

    }
}
